#include "MyBuffer.h"
#include <string.h>

MyBuffer::MyBuffer( const int p_nBufferSize /*= 1 * 1024*/ )
{
	m_szBuffer = new char[p_nBufferSize];
	m_nSize = p_nBufferSize;
	m_nUsed = 0;
}

MyBuffer::~MyBuffer()
{
	delete[] m_szBuffer;
}

bool MyBuffer::push(const char* p_szContent, const int p_nContentLen)
{
	if (m_nUsed + p_nContentLen > m_nSize)
	{
		return false;
	}
	memcpy(m_szBuffer + m_nUsed, p_szContent, p_nContentLen);
	m_nUsed += p_nContentLen;
	return true;
}

int MyBuffer::pop(char* p_pAllContent)
{
	int nPopLen = 0;

	if (m_nUsed == 0)
	{
		return 0;
	}
	if (p_pAllContent == NULL)
	{
		return -1;
	}
	memcpy(p_pAllContent, m_szBuffer, m_nUsed);
	nPopLen = m_nUsed;
	m_nUsed = 0;
	return nPopLen;
}
